package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.datadev.FlinkFunctionDetailResponse;
import com.sh.data.engine.application.model.dto.datadev.FlinkResourceListResponse;
import com.sh.data.engine.application.model.request.datadev.SaveFunctionNodeRequest;
import com.sh.data.engine.application.service.datadev.FlinkFunctionApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.datadev.flink.model.domain.FlinkDirTreeDomain;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Validated
@RestController
@RequestMapping("/flink/function")
@Tag(name = "实时开发函数管理API", description = "实时计算函数管理API")
@Slf4j
public class FlinkFunctionController {

    @Autowired
    private FlinkFunctionApplicationService flinkFunctionApplicationService;

    @PostMapping("/saveFuntionNode")
    @Operation(summary = "保存函数节点")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.实时任务函数)
//    @Log(
//        business = Business.实时任务函数,
//        operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> saveFunctionNode(@Valid SaveFunctionNodeRequest request) {
        var result = flinkFunctionApplicationService.saveFunctionNode(request);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getDirNodes")
    @Operation(summary = "获取目录树列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<List<FlinkDirTreeDomain>> getDirNodes(
        @Parameter(description = "类型,0-任务 1-资源 2-函数", required = true) @RequestParam Integer type) {
        var result = flinkFunctionApplicationService.getDirNodes(type);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/detail")
    @Operation(summary = "获取函数详情")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 0, isAdd = 1)
    public RestResponseEntity<FlinkFunctionDetailResponse> getFunctionDetail(
        @Parameter(description = "节点id", required = true) @RequestParam Long nodeId) {
        var result = flinkFunctionApplicationService.getFunctionDetail(nodeId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/resource/tree")
    @Operation(summary = "获取资源列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 0, isAdd = 1)
    public RestResponseEntity<List<FlinkResourceListResponse>> getAllNodes() {
        var result = flinkFunctionApplicationService.getAllNodes();
        return RestResponseEntity.success(result);
    }
}
